#!/usr/bin/env python

import os.path
import glob

import serial.serialutil
from twisted.internet import reactor,serialport
from hub import protocol,logging,web,config

def runServer(path):

    settings = config.Settings(path)
    logger = logging.Logger(path)

    for driver in glob.glob('/dev/tty.usbserial-*'):
        try:
            port = serialport.SerialPort(
                protocol.ModuleProtocol(logger,driver),driver,reactor)
        except serial.serialutil.SerialException:
            print 'Unable to establish serial communication with module at',driver

    reactor.listenTCP(8081,web.Site(logger,settings,path))

    reactor.run()
    
if __name__ == '__main__':
    myPath = os.path.dirname(__file__)
    serverPath,subPath = os.path.split(os.path.abspath(myPath))
    runServer(serverPath)